package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.teacher.dao.PassportDao;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.Passport;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLPassportInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.PassportInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddPassportMutation implements DataFetcher {

    @Autowired
    PassportDao passportDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 添加护照
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLPassportInput input = PassportInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        Passport passport = null;

        if (input.getId() != null) {
            passport = passportDao.get(Passport.class, input.getId());
            passport.setUpdatedAt(new Date());
        } else {
            passport = new Passport();
            passport.setCreatedAt(new Date());
        }
        if(schoolId != null){

            passport.setSchool_id(schoolId);
        }
        if(input.getTeacher_id() != null){

            passport.setTeacher_id(input.getTeacher_id());
        }
        if(input.getTime() != null){

            passport.setTime(input.getTime());
        }
        if(input.getEntime() != null){

            passport.setEntime(input.getEntime());
        }
        if(input.getGender() != null){

            passport.setGender(input.getGender());
        }
        if(input.getNation() != null){

            passport.setNation(input.getNation());
        }
        if(input.getIdcard() != null){

            passport.setIdcard(input.getIdcard());
        }
        if(input.getPassportnumber() != null){

            passport.setPassportnumber(input.getPassportnumber());
        }
        if(input.getCompanyopinion() != null){

            passport.setCompanyopinion(input.getCompanyopinion());
        }
        if(input.getCompanyopinionid() != null){

            passport.setCompanyopinionid(input.getCompanyopinionid());
        }
        if(input.getDirectorcompanyopinion() != null){

            passport.setDirectorcompanyopinion(input.getDirectorcompanyopinion());
        }
        if(input.getDirectorcompanyopinionid() != null){

            passport.setDirectorcompanyopinionid(input.getDirectorcompanyopinionid());
        }
        if(input.getCompanyopinionimg() != null){

            passport.setCompanyopinionimg(input.getCompanyopinionimg());
        }
        if(input.getDirectorcompanyopinionimg() != null){

            passport.setDirectorcompanyopinionimg(input.getDirectorcompanyopinionimg());
        }
        if(input.getName() != null){

            passport.setName(JSON.toJSONString(input.getName()));
        }
        if(input.getSourceofcost() != null){

            passport.setSourceofcost(JSON.toJSONString(input.getSourceofcost()));
        }
        if(input.getSourceofcostwa() != null){

            passport.setSourceofcostwa(JSON.toJSONString(input.getSourceofcostwa()));
        }
        if(input.getReasonsabroad() != null){

            passport.setReasonsabroad(JSON.toJSONString(input.getReasonsabroad()));
        }
        if(input.getNationality() != null){

            passport.setNationality(JSON.toJSONString(input.getNationality()));
        }
        try{
            passportDao.saveOrUpdate(passport);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

